package com.runtime.hongyunuser.service.Impl;
import com.runtime.hongyunuser.bean.Products;
import com.runtime.hongyunuser.bean.Shoppingcarts;
import com.runtime.hongyunuser.bean.vo.CartProductVO;
import com.runtime.hongyunuser.mapper.ProductMapper;
import com.runtime.hongyunuser.mapper.ShoppingcartsMapper;
import com.runtime.hongyunuser.service.ProductService;
import com.runtime.hongyunuser.service.ShoppingcartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ShoppingcartsServiceImpl implements ShoppingcartsService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ShoppingcartsMapper shoppingcartsMapper;

    @Autowired
    private ProductService productService;

    @Override
    public Shoppingcarts addToCart(Long userId, Long productId, Long quantity) {
        // 检查购物车中是否已有该商品
        Shoppingcarts existingItem = shoppingcartsMapper.findByUserIdAndProductId(userId, productId);

        if (existingItem != null) {
            // 如果已存在，增加数量
            existingItem.setQuantity(existingItem.getQuantity() + quantity);
//            existingItem.setAddedAt(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
            shoppingcartsMapper.update(existingItem);
            return existingItem;
        } else {
            // 如果不存在，创建新购物车项
            Products product = productService.getProductById(productId.intValue());
            if (product == null) {
                throw new RuntimeException("商品不存在");
            }

            Shoppingcarts newItem = new Shoppingcarts();
            newItem.setUserId(userId);
            newItem.setProductId(productId);
            newItem.setQuantity(quantity.longValue());
//            newItem.setAddedAt(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

            shoppingcartsMapper.save(newItem);
            return newItem;
        }
    }

    @Override
    public List<Shoppingcarts> getCartItems(Long userId) {
        return shoppingcartsMapper.getCartItems(userId);
    }

    @Override
    public void updateQuantity(Long cartId, Integer quantity) {
        Shoppingcarts cartItem = shoppingcartsMapper.findByCartId(cartId); // 需要在Mapper中添加此方法
        if (cartItem != null) {
            cartItem.setQuantity(quantity.longValue());
            cartItem.setAddedAt(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
            shoppingcartsMapper.update(cartItem);
        }
    }

    @Override
    public void removeFromCart(Long userId,Long productId) {
        shoppingcartsMapper.delete(userId, productId);
    }

    @Override
    public void clearCart(Long userId) {
        shoppingcartsMapper.clearCart(userId);
    }

    @Override
    public List<Products> getProductInfoFromCart(List<Shoppingcarts> cartItems) {
        List<Products> productList = new ArrayList<>();
        for (Shoppingcarts cartItem : cartItems) {
            Products product = productMapper.getProductById(cartItem.getProductId());
            productList.add(product);
        }
        return productList;
    }

    @Override
    public List<CartProductVO> getCartProductsByUserId(Long userId) {
        return shoppingcartsMapper.listCartProductsByUserId(userId);
    }

    @Override
    public int removeBatchFromCart(Long userId, List<Long> cartIds) {

        // 执行删除
        int affectedRows = shoppingcartsMapper.removeBatchFromCart(userId, cartIds);


        return affectedRows;
    }
}